VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "clsStopWatch"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'-----------------------------------------------------------------
'             PT DC Hub @ Direct Connect P2P Network
'-----------------------------------------------------------------
'       Developer: Carlos.DF (fLaSh) - Portugal
'          E-mail: carlosferreiracarlos@hotmail.com
' Project started: 10 - September - 2006
'         License: GNU General Public License.
'-----------------------------------------------------------------
'       Thanks to developers and contributores of SDCH/DDCH
'         The Left Hand, ButterflySoul, HaArD and Selyb
'  TheNOP, RollTheDice, JDommi, GhOstFaCE, ArchaicLight and TUFF
'-----------------------------------------------------------------
Option Explicit

' Win32 API declarations.
Private Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeEndPeriod Lib "winmm.dll" (ByVal uPeriod As Long) As Long
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function timeGetDevCaps Lib "winmm.dll" (lpTimeCaps As TIMECAPS, ByVal uSize As Long) As Long

' API Structure definitions.
Private Type TIMECAPS
    wPeriodMin As Long
    wPeriodMax As Long
End Type

' Set aside storage for private member variables.
Private m_StartTime As Long
Private m_PeriodMin As Long
Private m_PeriodMax As Long
' Desired timer resolution.
Private m_Resolution As Long

Private Sub Class_Initialize()
     'Retrieve system timer resolution.
2:    Dim TC As TIMECAPS
3:    On Error GoTo Err
      
5:    Call timeGetDevCaps(TC, Len(TC))

7:    m_PeriodMin = TC.wPeriodMin
8:    m_PeriodMax = TC.wPeriodMax
    
      'Set by defaut 1ms resolution.
11:   m_Resolution = 1
        
      'Reprogram timer chip resolution.
14:    Call timeBeginPeriod(m_Resolution)
    
      'Store starting time.
17:    Call Me.Reset

18:    Exit Sub
19:
Err:
20:    HandleError Err.Number, Err.Description, Erl & "|" & "clsStopWatch.Class_Initialize()"
End Sub

Public Property Get Resolution() As Long
1:    On Error GoTo Err
2:    Resolution = m_Resolution
3:    Exit Property
4:
Err:
5:    HandleError Err.Number, Err.Description, Erl & "|" & "clsStopWatch.Resolution()"
End Property

Public Property Let Resolution(lRes As Long)
1:    On Error GoTo Err
2:    If lRes <= 0 Then lRes = 1
3:    m_Resolution = lRes
4:    Exit Property
5:
Err:
6:    HandleError Err.Number, Err.Description, Erl & "|" & "clsStopWatch.Resolution(" & lRes & ")"
End Property

Public Property Get MinResolution() As Long
     ' Return: return minimum number of milliseconds
     '         timer is capable of resolving.
3:    On Error GoTo Err
4:    MinResolution = m_PeriodMin
5:    Exit Property
6:
Err:
7:    HandleError Err.Number, Err.Description, Erl & "|" & "clsStopWatch.MinimumResolution()"
End Property

Public Property Get MaxResolution() As Long
     ' Return: return maximum number of milliseconds
     '         timer is capable of resolving.
3:    On Error GoTo Err
4:    MaxResolution = m_PeriodMax
5:    Exit Property
6:
Err:
7:    HandleError Err.Number, Err.Description, Erl & "|" & "clsStopWatch.MaximumResolution()"
End Property

Public Sub Reset()
1:    On Error GoTo Err
      'Wait until next tick before storing start time.
3:    m_StartTime = timeGetTime()
4:    Do
        '
6:    Loop Until m_StartTime <> timeGetTime()
      'Initialize starting time.
8:    m_StartTime = timeGetTime()
9:    Exit Sub
10:
Err:
11:   HandleError Err.Number, Err.Description, Erl & "|" & "clsStopWatch.Reset()"
End Sub

Public Property Get Elapsed(Optional bFormatHMS As Boolean = False, Optional bShowDecimal As Boolean = False) As Variant 'Long or String
     ' Return: return elapsed time in milliseconds or formated time
     '         since stopwatch was reset.
3:    Dim lngElapsed As Long
4:    On Error GoTo Err

6:    lngElapsed = (timeGetTime() - m_StartTime)
      
8:    If Not bFormatHMS Then
9:        Elapsed = lngElapsed
10:   Else
          ' This routine is useful to get a nicely formatted display of
          ' elapsed time for reports and such.  Not a good one to call
          ' within tight loops, of course!
14:        Dim strRtn  As String
        
16:        strRtn = Format$(DateAdd("s", (lngElapsed \ 1000), #12:00:00 AM#), "HH:MM:SS")

18:        If bShowDecimal Then
19:            strRtn = strRtn & "." & Format$(lngElapsed Mod 1000, "000")
20:        End If
    
22:        Elapsed = strRtn
23:   End If
    
25:   Exit Property
26:
Err:
27:   HandleError Err.Number, Err.Description, Erl & "|" & "clsStopWatch.Elapsed(" & bFormatHMS & ", " & bShowDecimal & ")"
End Property
